<?php
if(!defined('ORP')) define('ORP', 'ORIGINAL_REQUEST_PARAMETER');
if(!defined('RC')) define('RC', 'REQUEST_CONTEXT');
if(!defined('TC')) define('TC', 'TEMPLATE_CONTEXT');

class Soul_Config{
	
	public static $debug = FALSE;
	public static $cacheDirectory = '';
	public static $templateDirectories = array();
	public static $handlerDirectories = array();
    public static $dbTablePrefix = '';
    public static $dbEncoding = 'utf8';
	public static $dynamicScopes = array();
    public static $dynamicPaths = array();
	public static $staticScopes = array();
	public static $includePaths = array();
	public static $proxyClasses = array();
    public static $domains = array();
    public static $dbConncetionStrings = array();
    
    private static $_defaultConnectionString = NULL;
	private static $_defaultDomain = NULL;
	
	public static function addProxyClass(){
		$args = func_get_args();
		self::$proxyClasses = array_merge(self::$proxyClasses, $args);
	}

	public static function addDynamicScope() {
		$args = func_get_args();
		if(is_array($args[0])){
			$args = $args[0];
		}
		self::$dynamicScopes = array_merge($args, self::$dynamicScopes);
	}
	
	public static function addStaticScope() {
		$args = func_get_args();
		if(is_array($args[0])){
			$args = $args[0];
		}
		self::$staticScopes = array_merge($args, self::$staticScopes);
	}		
    
	public static function addDynamicPath() {
		$args = func_get_args();
		if(is_array($args[0])){
			$args = $args[0];
		}
		self::$dynamicPaths = array_merge($args, self::$dynamicPaths);
	}    

	public static function addIncludePath() {
		$arr = func_get_args();
		if (!empty(self::$includePaths)) {
			$oldIncludePath = implode(PATH_SEPARATOR, self::$includePaths);
			self::$includePaths = array_merge($arr, self::$includePaths);
			$newIncludePath = implode(PATH_SEPARATOR, self::$includePaths);
			ini_set('include_path', str_replace($oldIncludePath, $newIncludePath, ini_get('include_path')));
		} else {
			self::$includePaths = $arr;
			$ps = implode(PATH_SEPARATOR, $arr);
			ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . $ps);
		}
	}	
	
    public static function addTemplateDirectory($path){
		self::$templateDirectories[] = $path;
    }
    
    public static function addHandlerDirectory($path){
		self::$handlerDirectories[] = $path;
    }    
	
    public static function addConnectionString($name, $driver, $server, $database, $username, $password, $isDefault = FALSE){
        self::$dbConncetionStrings[] = array(
            'name'=>$name,
            'driver'=>$driver,
            'server'=>$server,
            'database'=>$database,
            'username'=>$username,
            'password'=>$password
        );
        if($isDefault){
            self::$_defaultConnectionString = array(
	            'name'=>$name,
	            'driver'=>$driver,
	            'server'=>$server,
	            'database'=>$database,
	            'username'=>$username,
	            'password'=>$password
        	);
        }
    }
    
    public static function getConnectionString($name = NULL, $returnDefault = FALSE){
    	if(!empty($name)){
    		foreach(self::$dbConncetionStrings as $connection){
    			if($connection[$name] == $name){
	        		return $connection;
    			}
    		}
    	}
        if($returnDefault){
        	return self::$_defaultConnectionString;
        }
        return NULL;
    }   	

	public static function addDomain($name, $hostName, $baseUrl = '/', $subName = 'www', $default = FALSE){
		$d = Soul_Loader::loadInstance('Soul_Request_Http_Domain')
				->setName($name)
				->setMainName($hostName)
				->setSubName($subName)
				->setBaseUrl($baseUrl);
		self::$domains[] = $d;
		if($default){
			self::$_defaultDomain = $d;
		}
	}	

	public static function getDomain($name = NULL, $returnDefault = FALSE){
		if(!empty($name)){
			foreach(self::$domains as $domain){
				if($domain->getName() == $name){
					return $domain;
				}
			}
		}

		if($returnDefault){
			return self::$_defaultDomain;
		}
		return NULL;
	}
}